package com.android.aishangauction.fragment.group;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.aishangauction.fragment.BaseLazyFragment;
import com.android.aishangauction.utils.ObjectUtils;
import com.android.aishangauction.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * WebView展示
 */
public class Fragment_Main_WebView extends BaseLazyFragment {


    @BindView(R.id.progressBar_h)
    ProgressBar progressBarH;
    @BindView(R.id.webView1)
    WebView webView1;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    private FragmentActivity mcontext;
    private View view;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String webUrl;
    private String detail_content;


    public Fragment_Main_WebView() {
    }
    CallBackValue callBackValue;
    @OnClick(R.id.close)
    public void onClick() {
        mcontext.finish();
    }

    public interface CallBackValue {
        void SendMessageValue(WebView webview);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        callBackValue = (CallBackValue) getActivity();
    }

    public static Fragment_Main_WebView newInstance(String param1, String web_content) {
        Fragment_Main_WebView fragment = new Fragment_Main_WebView();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, web_content);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            webUrl = getArguments().getString(ARG_PARAM1);
            detail_content = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    private void initUI() {


        WebSettings settings = webView1.getSettings();
        settings.setJavaScriptEnabled(true);
        callBackValue.SendMessageValue(webView1);
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {

        if (!ObjectUtils.isNetworkConnected(mcontext)) {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");
            return;
        }
        webView1.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 0) {
                    progress.setVisibility(View.VISIBLE);
                    progressBarH.setVisibility(View.VISIBLE);
                }
                progressBarH.setProgress(newProgress);
                progress.postInvalidate();
                progressBarH.postInvalidate();
                if (newProgress == 100) {
                    progress.setVisibility(View.GONE);
                    progressBarH.setVisibility(View.GONE);
                }
            }
        });
        if (webUrl.equals("")) {
            if (detail_content != null && !detail_content.equals("")) {
                //宽度适应屏幕
                String js = "<style>\n" +
                        "img{\n" +
                        " max-width:100%;\n" +
                        "height:auto\n" +
                        "}\n" +
                        "</style>" + detail_content;

                webView1.loadDataWithBaseURL(null, js, "text/html", "utf-8", null);
            } else {
                mcontext.finish();
                progress.setVisibility(View.GONE);
                ObjectUtils.toast(mcontext, "无内容");
            }
        } else {

            webView1.setWebViewClient(new MyWebViewClient());
            if (!"".equals(webUrl)) {
                webView1.loadUrl(webUrl);
            }
        }


    }


    class MyWebViewClient extends WebViewClient {
        //拦截网址
        @Override

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            //如果不需要其他对点击链接事件的处理返回true，否则返回false
            return true;

        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_main_webview, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }


}
